
package com.probiz.estore.imports.handler.product;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;
import com.probiz.estore.product.service.ProductSkuManager;

public class ProductSkuCodeHandler extends ColumnBasicHandler implements
		ColumnHandler {
	private ProductSkuManager	productSkuManager	= null;
	private Logger logger = Logger.getLogger(ProductSkuCodeHandler.class);
	

	public void setProperty(ImportModel importModel,Column column) throws Exception {
		String value = column.getValue();
		Product product = (Product)importModel.getTarget();
		if (StringUtils.isEmpty(value)) {
			return;
		}	
		ProductSku productSku = productSkuManager.getProductSkuByProductSkuCode(value);
		if(productSku!=null){
			//必须检查一下这个货品是否属于这个商品
			if(!productSku.getProductId().equals(product.getProductId())){
				//设置结果
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "无法导入，货号【"+productSku.getProductSkuCode()+"】并不属于商品编号为【"+product.getProductCode()+"】的商品，请检查。";
				logger.error(msg);
				importModel.setMsg(msg);
				return;
			}
			//start:将相关的延迟加载对象预先读取
			productSku.getInventory().getQuantityOnHand();
			//end:  将相关的延迟加载对象预先读取
		}else{
			productSku = new ProductSku();
			productSku.setProductSkuCode(value);
		}
		product.setImportProductSku(productSku);
		importModel.setResult(ImportModel.RESULT_SUCCESS);
	}


	public void setProductSkuManager(ProductSkuManager productSkuManager) {
		this.productSkuManager = productSkuManager;
	}
}
